package node_battle

import (
	"fmt"
	"sanguo/cs"
	"strconv"
	"strings"
	"sanguo/node/common/db"
	"github.com/sniperHW/kendynet/event"
	"sanguo/rpc/createRoom"
)

func Start(externalAddr string) error {
	t := strings.Split(externalAddr, ":")
	port, _ := strconv.Atoi(t[1])
	return cs.StartTcpServer("tcp", fmt.Sprintf("0.0.0.0:%d", port), &Dispatcher)
}

func Init(redisAddr string) {
	queueIterator = 0
	roomID = 1
	queueCount = 10
	roomTimer = 30

	db.RedisInit(strings.Split(redisAddr, ","))
	redisQueue = event.NewEventQueue()
	go redisCron(redisQueue)

	eventQueueList = []*event.EventQueue{}
	roomList = make([]map[int64]Room, queueCount)
	allUsers = map[uint64]uint64{}

	var i int64
	for i =0; i < queueCount; i++ {
		roomList[i] = map[int64]Room{}
		eventQueue := event.NewEventQueue()
		eventQueueList = append(eventQueueList, eventQueue)
		go queueCron(eventQueue)
	}

	Dispatcher.handlers = map[string]handler{}
	//Dispatcher.Register(&ss_rpc.CreateRoomReq{}, onCreateRoom)
	StatusDispatcher.handlers = map[int32]statusHandler{}
	StatusDispatcher.Register(Status1, xxStatus)
	createRoom.Register(&CreateRoom{})
	go timerCron()
}
